package org.tio.sitexxx.im.server.handler;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.sitexxx.im.common.Command;
import org.tio.sitexxx.im.common.CommandHandler;
import org.tio.sitexxx.im.common.ImPacket;
import org.tio.sitexxx.im.common.bs.P2PNotReadCountResp;
import org.tio.sitexxx.im.server.Ims;
import org.tio.sitexxx.im.server.utils.ImUtils;
import org.tio.sitexxx.service.model.main.User;
import org.tio.sitexxx.service.service.stat.ChatRecordP2PService;

import com.jfinal.plugin.activerecord.Record;

/**
 * 查询未读私聊消息数请求
 * @author tanyaowu 
 * 2016年9月13日 上午10:16:19
 */
@CommandHandler(Command.P2P_NOT_READ_COUNT_REQ)
public class P2PNotReadCountReqHandler implements ImServerHandler {
	@SuppressWarnings("unused")
	private static Logger log = LoggerFactory.getLogger(P2PNotReadCountReqHandler.class);

	public static final P2PNotReadCountReqHandler me = new P2PNotReadCountReqHandler();

	public P2PNotReadCountReqHandler() {
	}

	@Override
	public void handler(ImPacket packet, ChannelContext channelContext, boolean isWebsocket) throws Exception {
		User curr = ImUtils.getUser(channelContext);
		if (curr == null) {
//			ImUtils.pleaseLogin(channelContext, "登录才能聊天哦");
			return;
		}
		
		List<Record> list = ChatRecordP2PService.me.getLastN2pChat(curr.getId());//.getNotReadCount(curr.getId());
		
		if (list != null && list.size() > 0) {
			P2PNotReadCountResp p2pNotReadCountResp = new P2PNotReadCountResp();
			p2pNotReadCountResp.setList(list);
			
			ImPacket imPacket = new ImPacket(Command.P2P_NOT_READ_COUNT_RESP, p2pNotReadCountResp);
			Ims.send(channelContext, imPacket );
		}
	}

}
